<script>
	$(function(){
		PageAction.editor.create(['substance', 'specialty'], { width : '100%', height : '320px' });

		PageAction.currentPageActionClassName = '<?= $_GET['currentPageActionClassName'] ?>';
		PageAction.currentPageAction = '<?= ucfirst($_GET['action']) ?>';

		if (typeof PageAction[PageAction.currentPageActionClassName].doContentAction == 'undefined') {
			PageAction[PageAction.currentPageActionClassName].doContentAction = function() {
				var _test = Tools.checkNull('title', '请输入信息标题') && Tools.checkNull('sort', '请输入排序') &&
					Tools.compare('category_id', 0, '请选择所属类别');
				if (_test) {
					$.dialog.locking('系统已启动，请稍候。。。');

                    //建议搭配json
                    var _linkArr = {};
                    var _i = 0;
                    $("#subject tr").each(function () {
                        _linkArr[_i] = $(this).find('button').attr('data-id');
                        _i++;
                    });
                    var _str = JSON.stringify(_linkArr);
                    $('#subject2').val(_str);

					PageAction.editor.sync();

					var _this = this;
					$.post(this.handlerUrl + PageAction.currentPageAction,
					Tools.addEditPageLoader().DOM.form.serializeArray(),
					function(data) {
						switch (data.status) {
							case 'SUCCESS' :
								$(_this.grid).dataGridReload();

								$.dialog.locking.confirm('success', '编辑成功，您还需要继续添加吗？', function(){

									Tools.addEditPageLoader()
									.resetConfig({
										content : 'load:' + _this.addEditPage + '&action=add'
											+ '&parentId=' + $('#category_id').val()
									}, true);

									return true;
								}, function(){
									Tools.addEditPageLoader().close();
									return true;
								});

								break;
							case 'NO_CHANGES' :
								$.dialog.locking.alert('ndash', data.desc);
								break;
							case 'DB_ERROR' :
							case 'INTERCEPTOR' :
							case 'SEO_URL_IS_EXISTS' :
								$.dialog.locking.alert('error', data.desc);
								break;
							default :
								$.dialog.locking.alert('error', '系统繁忙，请稍候再试');
								break;
						}
					}, 'json');
				}
			};
		}

		Tools.addEditPageLoader()
		.title('<?= $this->adminMap[$this->security]['title'] ?> » <?= $this->adminMap[$this->security]['menu'][$_GET['parentPageId']]['name'] ?> » <?= App\Admin\Helper::getActionName($_GET['action']) ?>');

		<?php
		if ($this->pageControl) {
		?>
		Tools.addEditPageLoader().button({
			id : 'ok',
			name : Tools.addEditPageLoader().config.okVal,
			callback : function(){
				
				PageAction[PageAction.currentPageActionClassName]
				.doContentAction
				.call(PageAction[PageAction.currentPageActionClassName]);
				
				return false;
			},
			unshift : true,
			type : 'submit',
			focus: true
		});
		<?php
		}
		?>
		Tools.switchDiv('.switch > ul > li', 'slt_switch', '.switch_list');

		$('.tags > ol > li > span').click(function(){
			var _archivesTags = $('#archives_tags').val();
			var _tag = $(this).text();

			if (_archivesTags.indexOf(_tag) == -1) {
				if (_archivesTags.trim().length > 0) {
					_archivesTags += ',';
				}
				_archivesTags += _tag;
			}
			$('#archives_tags').val(_archivesTags);
		});
	});
</script>

<input type="hidden" name="id" id="id" value="<?= $_GET['id'] ?>">
<input type="hidden" name="subject2" id="subject2" value="">
<div class="switch">
	<ul>
		<li class="slt_switch">基本信息</li>
		<li>产品简介</li>
		<li>产品影像</li>
        <li>系列产品</li>
		<li>多图上传</li>
	</ul>
	<div class="switch_list">
		<table width="100%" cellpadding="0" cellspacing="0" class="data_input">
			<tr>
				<th width="15%">语言</th>
				<td width="35%"><?= App\Tools\Html::radio($this->pageControl, 'language', $this->rs, $this->__LANGUAGE_CONFIG__, $this->__LANGUAGE_ID__) ?></td>
				<th width="15%">修改时间</th>
				<td><?= App\Tools\Html::setDate($this->pageControl, 'release_date', App\Tools\Auxi::getTime(time()), '90%') ?></td>
			</tr>
			<tr>
				<th>产品标题</th>
				<td><?= App\Tools\Html::text($this->pageControl, 'title', $this->rs) ?></td>
				<th>所属类别</th>
				<td><?= $this->sltIDTree ?></td>
			</tr>
			<tr>
				<th>产品简介</th>
				<td><?= App\Tools\Html::textarea($this->pageControl, 'synopsis', $this->rs, null, 5, null, ' onpropertychange="if(value.length>200) value=value.substr(0,200)"') ?></td>
				<th>信息相关图
					<br />
					(缩略图)
					<br/>建议尺寸:
					<br/>(670*450)
				</th>
				<td><?= App\Admin\Helper::createUpFile('img', 'cover', $this->rs ? $this->rs->cover : null,
						$this->setting['aryPicExtName'], $this->setting['aryFileExtName'], $this->__CDN__, $this->__ASSETS__) ?></td>
			</tr>
			<tr>
				<th>是否首页显示</th>
				<td>
					<?= App\Tools\Html::radio($this->pageControl, 'is_home_display', $this->rs, $this->setting['aryBool'], '1', 'horizontal') ?>
				</td>
				<th>是否导航显示</th>
				<td>
				<?= App\Tools\Html::radio($this->pageControl, 'is_column', $this->rs, $this->setting['aryBool'], '1', 'horizontal') ?>
				</td>
			</tr>
			<tr>
				<th>排序<br/>(数值越大越靠前)</th>
				<td><?= App\Tools\Html::text($this->pageControl, 'sort', $this->rs ? $this->rs : $this->getSort) ?></td>
				<th rowspan="3">Seo Description</th>
				<td rowspan="3"><?= App\Tools\Html::textarea($this->pageControl, 'seo_description', $this->rs, null, 4) ?></td>
			</tr>
			<tr>
				<th>Seo Title</th>
				<td><?= App\Tools\Html::text($this->pageControl, 'seo_title', $this->rs) ?></td>
			</tr>
			<tr>
				<th>Seo Keywords</th>
				<td><?= App\Tools\Html::textarea($this->pageControl, 'seo_keywords', $this->rs) ?></td>
			</tr>
		</table>
	</div>
	<div class="switch_list hide">
		<table width="100%" cellpadding="0" cellspacing="0" class="data_input">
			<tr>
				<th width="15%">产品简介</th>
				<td height="320"><?= App\Tools\Html::editor($this->pageControl, 'substance', $this->rs) ?></td>
			</tr>
		</table>
	</div>
    <div class="switch_list hide">
		<table width="100%" cellpadding="0" cellspacing="0" class="data_input">
			<tr>
				<th width="15%">产品简介</th>
				<td height="320"><?= App\Tools\Html::editor($this->pageControl, 'specialty', $this->rs) ?></td>
			</tr>
		</table>
	</div>
    <div class="switch_list hide" style="height:400px;overflow-y:scroll">
        建议搭配:(仅限5个)
        <table width="100%" cellpadding="0" cellspacing="0" class="data_input" id="subject">
            <tr>
                <td>产品ID</td>
                <td>产品名称</td>
                <td>操作</td>
            </tr>
            <?php
            if ($this->subject) :
                foreach($this->subject as $val):?>
                    <tr><td><?= $val->archives_id ?></td><td><?= $val->title ?></td><td><button class="del_link" data-id="<?= $val->archives_id ?>">删除关联</button></td></tr>
                    <?php
                endforeach;
            endif;
            ?>
        </table>
        <input type="search" placeholder="请输入产品名称" size="30" id="searchKey"/>
        <button id="search">搜索</button>
        <table width="100%" cellpadding="0" cellspacing="0" class="data_input" id="search_subject">

        </table>
    </div>
	<div class="switch_list hide">
		<div class="multi-upload-container" id="multi-upload-container1">
		</div>
	</div>
</div>
<script>
	$(function () {
		//多图上传
		function loadCss(url) {
			var s = document.createElement("LINK");
			s.rel = "stylesheet";
			s.type = "text/css";
			s.href = url;
			document.getElementsByTagName("HEAD")[0].appendChild(s);
		}

		// 加载一次js,css
		if (PageAction[PageAction.currentPageActionClassName].UPLOADSWF_LOAD_FLAG === false) {
			PageAction[PageAction.currentPageActionClassName].UPLOADSWF_LOAD_FLAG = true;
			loadCss('<?= $this->__STATIC__ ?>js/multiUpload/style/css/default.css');
			$.getScript("<?= $this->__STATIC__ ?>js/multiUpload/jquery.uploadify.min.js");
			$.getScript("<?= $this->__STATIC__ ?>js/multiUpload/myhandler.js");
		}
		setTimeout(function () {
			var banners = <?= $this->banners ? $this->banners : '[]' ?>;

			$('#multi-upload-container1').multiUpload({
				'coverField': 'src', // 主表中封面字段名
				subTable: [// 副表中的字段, 需要录入的
					{fieldName: 'src', display: 'Banner标题', type: 'input'},
				],
				defaultVal: banners,
				RecommendSize: '推荐尺寸(670*450)',
				type : 2,
				uploadAction: 'upload-action',
				imgAdd: 'img-add',
				imgDiv: 'multi-upload-container1',
				multiEditorBox: 'multi-editor-box'

			});

		}, 500);

	});
</script>
<script>
    //建议搭配
    $("#search").click(function(){
        var key = $("#searchKey").val();
        var language = $(":input[name=language]").val();
        if(key){
            ajaxSearch(key, language);
        }
    });

    function ajaxSearch(key,language) {
        $.post("/handler/Admin.Structure.Product.SearchSubject",
            {key: key,language:language},
            function(data) {
                if (data.status == 'SUCCESS'){
                    var _out = '';
                    for(var i in data.rsp.rs) {
                        var m = data.rsp.rs[i];
                        _out += '<tr><td>' + m.archives_id + '</td><td>' + m.title + '</td><td><button class="add_link" data-id="' + m.archives_id + '">添加关联</button></td></tr>';
                    }
                    $("#search_subject").empty();
                    $("#search_subject").append(_out);
                }
            }, 'json');
    }

    $('body').on('click','.add_link',function(){
        var html = $(this).parent().parent().html();
        html = html.replace('add_link','del_link');
        html = html.replace('添加关联','删除关联');
        var link_html = ($("#subject").html());
        if(link_html.indexOf(html) < 0 && $("#subject button").size() < 5){
            html = '<tr>' + html + '</tr>';
            $("#subject").append(html);
        }
    });

    $('body').on('click','.del_link',function(){
        $(this).parent("td").parent("tr").remove();
    });
</script>
